/**
 * 
 */
package com.iwords.service.persistence.impl;

import com.iwords.service.ao.impl.AbstractFacade;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;

import com.iwords.service.object.SysLibWordsEntity;
import com.iwords.service.object.TeacherEntity;
import com.iwords.service.persistence.TeacherPersistence;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author    	huangbaichuan 
 * @time      	2011-8-14    
 * @description	  
 */
@Stateless
public class TeacherPersistenceImpl extends AbstractFacade<TeacherEntity> implements TeacherPersistence {
        Log logger = LogFactory.getLog(this.getClass());

    @PersistenceContext(unitName = "iwords")
    private EntityManager em;

    public TeacherPersistenceImpl() {
        super(TeacherEntity.class);
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
    @SuppressWarnings({"unchecked", "rawtypes"})
    public List<TeacherEntity> findAll() {
        CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(SysLibWordsEntity.class));
        return getEntityManager().createQuery(cq).getResultList();
    }

    @SuppressWarnings("unchecked")
    public List<TeacherEntity> findRange(int[] range) {
        CriteriaQuery<Object> cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(SysLibWordsEntity.class));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return (List<TeacherEntity>) q.getResultList();
    }

    public int count() {
        CriteriaQuery<Object> cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<SysLibWordsEntity> rt = cq.from(SysLibWordsEntity.class);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

    public TeacherEntity findUser(TeacherEntity entity) {

        Query query = getEntityManager().createQuery("SELECT u from TeacherEntity u where  u.teacherid=?1 and u.teachername=?2");
        try {
            query.setParameter(1, entity.getTeacherid());
            query.setParameter(2, entity.getTeachername());
            return (TeacherEntity) query.getSingleResult();

        } catch (NoResultException e) {
        }
        return null;
    }

    public boolean isContain(TeacherEntity entity) {
        Query query = getEntityManager().createQuery("SELECT count(u) from TeacherEntity u where  u.marketingid=?1");
        try {
            query.setParameter(1, entity.getMarketingid());
            Long count = (Long)query.getSingleResult();
            if (count.longValue()>0) {
                return true;
            }

        } catch (NoResultException e) {
        }
        return false;
    }

    @Override
    public TeacherEntity findTeacherByMarktingID(TeacherEntity entity) {
        Query query = getEntityManager().createQuery("SELECT u from TeacherEntity u where  u.marketingid=?1");

        query.setParameter(1, entity.getMarketingid());
        try{
           return (TeacherEntity)query.getSingleResult();
        }catch(Exception e){
            logger.error("error", e);
            return null;
        }

    }
}
